package com.example.linktop.logindemo;

import android.content.Context;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by linktoplinktop on 2017/5/22.
 * 保存用户名密码
 */
public class UserInfoUtils {

    private static final String filepath =  "/data/data/com.example.linktop.logindemo/info.txt";


    public static boolean saveInfo(Context ctx, String str_usrName, String str_pwd) {


        try {
            String result = str_usrName + "##" + str_pwd;

//            String ctxpath =  ctx.getFilesDir().getPath();
//
//
//
//            File file = new File(ctxpath,"info.txt");
//            FileOutputStream fio = new FileOutputStream(file);
            FileOutputStream fio = ctx.openFileOutput("info.txt", Context.MODE_PRIVATE);
            fio.write(result.getBytes());
            fio.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;

        }


    }

    public static boolean saveInfo(String str_usrName, String str_pwd) {


        try {
            String result = str_usrName + "##" + str_pwd;


            File file = new File(filepath);
            FileOutputStream fio = new FileOutputStream(file);
            fio.write(result.getBytes());
            fio.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;

        }


    }

    public static Map<String, String> readInfo() {

        try {
            Map<String, String> map = new HashMap<String, String>();

            File file = new File(filepath);
            FileInputStream fip = new FileInputStream(file);
            BufferedReader reader = new BufferedReader(new InputStreamReader(fip));
            String content = reader.readLine();

            //分隔
            String[] list = content.split("##");

            String name = list[0];
            String pwd = list[1];

            map.put("name", name);
            map.put("pwd", pwd);
            fip.close();

            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
    public static Map<String, String> readInfo(Context ctx) {

        try {
            Map<String, String> map = new HashMap<String, String>();

//            String path = ctx.getFilesDir().getPath();
//
//            File file = new File(path);
//            FileInputStream fip = new FileInputStream(file);
            FileInputStream fip = ctx.openFileInput("info.txt");

            BufferedReader reader = new BufferedReader(new InputStreamReader(fip));
            String content = reader.readLine();

            //分隔
            String[] list = content.split("##");

            String name = list[0];
            String pwd = list[1];

            map.put("name", name);
            map.put("pwd", pwd);
            fip.close();

            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }


}
